﻿using Dapper;
using System;
using BCEntity.OA.WorkflowActive.WorkflowActivityStepCompleteChecking;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.OA.WorkflowActive.WorkflowActivityStepCompleteChecking
{
	/// <summary>
	/// 工作流活动步骤完成检查
	/// </summary>
	public class WorkflowActivityStepCompleteCheckingData : IWorkflowActivityStepCompleteCheckingData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM oa_workflowactivitystepcompletechecking";
		private const string QUERY = @"SELECT WorkflowActivityStepCompleteCheckingId,OpertionName,OpertionType,FormConfigCode,AllowIgnore,OpertionStatus,CreateTime,WorkflowActivityStepId,
											WorkflowActivityId,TemplateCode,TemplateName,WorkflowCode,WorkflowName,StepCode,StepOrder,OperationTime,TemplateStepCode
										FROM oa_workflowactivitystepcompletechecking";
		public WorkflowActivityStepCompleteCheckingData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查工作流活动步骤完成检查是否存在
		public async Task<bool> Exists(long workflowActivityStepCompleteCheckingId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowActivityStepCompleteCheckingId=?WorkflowActivityStepCompleteCheckingId", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepCompleteCheckingId", workflowActivityStepCompleteCheckingId, DbType.Int64);
			return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加工作流活动步骤完成检查
		public async Task<WorkflowActivityStepCompleteCheckingEntity> Add(WorkflowActivityStepCompleteCheckingEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowactivitystepcompletechecking", new string[] { "OpertionName", "OpertionType", "FormConfigCode", "AllowIgnore", "OpertionStatus", 
				"WorkflowActivityStepId", "WorkflowActivityId", "TemplateCode", "TemplateName", "WorkflowCode", "WorkflowName", "StepCode", "StepOrder","TemplateStepCode" });
			sql = string.Format("{0} {1} WHERE WorkflowActivityStepCompleteCheckingId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?OpertionName", entity.OpertionName?.Trim(), DbType.AnsiString, size: 200);
			parameters.Add("?OpertionType", entity.OpertionType?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?AllowIgnore", entity.AllowIgnore, DbType.Boolean);
			parameters.Add("?OpertionStatus", entity.OpertionStatus.ToString(), DbType.AnsiString);
			parameters.Add("?WorkflowActivityStepId", entity.WorkflowActivityStepId, DbType.Int64, size: 20);
			parameters.Add("?WorkflowActivityId", entity.WorkflowActivityId, DbType.Int64, size: 20);
			parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?WorkflowCode", entity.WorkflowCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?WorkflowName", entity.WorkflowName?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?StepCode", entity.StepCode?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?StepOrder", entity.StepOrder, DbType.Int32, size: 11);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WorkflowActivityStepCompleteCheckingEntity>(sql, parameters, databaseContext.Transaction);

		}

		public async Task<bool> Add(List<WorkflowActivityStepCompleteCheckingEntity> entities)
		{
			string sql = SqlCommands.GetInsertSql("oa_workflowactivitystepcompletechecking", new string[] { "OpertionName", "OpertionType", "FormConfigCode", "AllowIgnore", "OpertionStatus",
				"WorkflowActivityStepId", "WorkflowActivityId", "TemplateCode", "TemplateName", "WorkflowCode", "WorkflowName", "StepCode", "StepOrder","TemplateStepCode" });
			List<DynamicParameters> dynamicParameters = new List<DynamicParameters>();
			foreach (var entity in entities)
			{
				DynamicParameters parameters = new DynamicParameters();
				parameters.Add("?OpertionName", entity.OpertionName?.Trim(), DbType.AnsiString, size: 200);
				parameters.Add("?OpertionType", entity.OpertionType?.Trim(), DbType.AnsiString, size: 50);
				parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
				parameters.Add("?AllowIgnore", entity.AllowIgnore, DbType.Boolean);
				parameters.Add("?OpertionStatus", entity.OpertionStatus.ToString(), DbType.AnsiString);
				parameters.Add("?WorkflowActivityStepId", entity.WorkflowActivityStepId, DbType.Int64, size: 20);
				parameters.Add("?WorkflowActivityId", entity.WorkflowActivityId, DbType.Int64, size: 20);
				parameters.Add("?TemplateCode", entity.TemplateCode?.Trim(), DbType.AnsiString, size: 50);
				parameters.Add("?TemplateName", entity.TemplateName?.Trim(), DbType.AnsiString, size: 100);
				parameters.Add("?WorkflowCode", entity.WorkflowCode?.Trim(), DbType.AnsiString, size: 50);
				parameters.Add("?WorkflowName", entity.WorkflowName?.Trim(), DbType.AnsiString, size: 100);
				parameters.Add("?TemplateStepCode", entity.TemplateStepCode?.Trim(), DbType.AnsiString, size: 50);
				parameters.Add("?StepCode", entity.StepCode?.Trim(), DbType.AnsiString, size: 50);
				parameters.Add("?StepOrder", entity.StepOrder, DbType.Int32, size: 11);
				dynamicParameters.Add(parameters);
			}
			int result = await this.databaseContext.Master.ExecuteAsync(sql,dynamicParameters, databaseContext.Transaction);
			return result == entities.Count;
		}
		#endregion

		#region 修改工作流活动步骤操作状态
		public async Task<bool> UpdateStatusForFormSubmit(WorkflowActivityStepCompleteCheckingEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowactivitystepcompletechecking", new string[] { "OpertionStatus", "OperationTime" },
				new string[] { "WorkflowActivityStepId", "FormConfigCode" });
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepId", entity.WorkflowActivityStepId, DbType.Int64, size: 20);
			parameters.Add("?FormConfigCode", entity.FormConfigCode?.Trim(), DbType.AnsiString, size: 100);
			parameters.Add("?OpertionStatus", entity.OpertionStatus.ToString(), DbType.AnsiString);
			parameters.Add("?OperationTime", DateTime.Now, DbType.DateTime);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}

		public async Task<bool> UpdateStatusForOperationType(WorkflowActivityStepCompleteCheckingEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("oa_workflowactivitystepcompletechecking", new string[] { "OpertionStatus", "OperationTime" },
				new string[] { "WorkflowActivityStepId", "OpertionType" });
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepId", entity.WorkflowActivityStepId, DbType.Int64, size: 20);
			parameters.Add("?OpertionType", entity.OpertionType?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?OpertionStatus", entity.OpertionStatus.ToString(), DbType.AnsiString);
			parameters.Add("?OperationTime", DateTime.Now, DbType.DateTime);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 删除工作流活动步骤完成检查
		public async Task<bool> Delete(long workflowActivityStepCompleteCheckingId)
		{
			string sql = @"DELETE FROM oa_workflowactivitystepcompletechecking WHERE WorkflowActivityStepCompleteCheckingId=?WorkflowActivityStepCompleteCheckingId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepCompleteCheckingId", workflowActivityStepCompleteCheckingId, DbType.Int64);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 获取工作流活动步骤完成检查
		public async Task<WorkflowActivityStepCompleteCheckingEntity> GetEntity(long workflowActivityStepCompleteCheckingId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowActivityStepCompleteCheckingId=?WorkflowActivityStepCompleteCheckingId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepCompleteCheckingId", workflowActivityStepCompleteCheckingId, DbType.Int64);
			return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WorkflowActivityStepCompleteCheckingEntity>(sql, parameters);
		}
		#endregion

		#region 获取工作流活动步骤完成检查列表
		public async Task<IEnumerable<WorkflowActivityStepCompleteCheckingEntity>> GetList(long workflowActivityStepId)
		{
			string sql = string.Format(@"{0} WHERE WorkflowActivityStepId=?WorkflowActivityStepId", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?WorkflowActivityStepId", workflowActivityStepId, DbType.Int64);
			return await this.databaseContext.Slave.QueryAsync<WorkflowActivityStepCompleteCheckingEntity>(sql,parameters);
		}
		#endregion

		#region 获取工作流活动步骤完成检查分页
		public async Task<(IEnumerable<WorkflowActivityStepCompleteCheckingEntity>, int)> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();

			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM oa_workflowactivitystepcompletechecking {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.Slave.QueryMultipleAsync<WorkflowActivityStepCompleteCheckingEntity, int>(sql, parameters);
		}
        #endregion
    }
}



